package com.drinks.yiniang.ui.activity.user.order;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.viewpager.CodeTabFragmentAdapter;
import com.drinks.yiniang.common.listener.OnOrderListChangeCallback;
import com.drinks.yiniang.ui.fragment.user.order.MyOrderFragment;
import com.drinks.yiniang.ui.widget.dialog.ContactServiceDialog;
import com.hansen.library.Constants;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.pickerview.bean.CodeValueBean;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的订单页面
 * @author han
 * @date 2019/09/06
 */
public class MyOrderActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        TabLayout.OnTabSelectedListener, OnOrderListChangeCallback {

    //nav
    private NavigationBarLayout nav_bar_my_order;
    //tab
    private TabLayout tab_my_order;
    //view pager
    private ViewPager vp_my_order;

    private CodeTabFragmentAdapter mTabFragmentAdapter;

    private List<BaseLazyFragment> mFragments;

    private List<CodeValueBean> mTabs;

    private String mCurrentOrderType;

    @Override
    protected void initView() {
        nav_bar_my_order = findViewById(R.id.nav_bar_my_order);

        tab_my_order = findViewById(R.id.tab_my_order);

        vp_my_order = findViewById(R.id.vp_my_order);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mCurrentOrderType = getIntent() != null ? getIntent().getStringExtra(Constants.KeyOrderType) : null;

        initTabAndAdapter();
    }

    private void initTabAndAdapter() {
        mTabs = new ArrayList<>(5);

        mTabs.add(new CodeValueBean(Constants.TYPE_ORDER_ALL, getString(R.string.text_all_order)));
        mTabs.add(new CodeValueBean(Constants.TYPE_ORDER_WAIT_FOR_PAY, getString(R.string.text_wait_for_pay)));
        mTabs.add(new CodeValueBean(Constants.TYPE_ORDER_WAIT_FOR_DELIVER, getString(R.string.text_wait_for_deliver_servicing)));
        mTabs.add(new CodeValueBean(Constants.TYPE_ORDER_WAIT_FOR_RECEIPT, getString(R.string.text_wait_for_receipt)));
        mTabs.add(new CodeValueBean(Constants.TYPE_ORDER_WAIT_FOR_EVALUATE, getString(R.string.text_wait_for_evaluate)));

        mFragments = new ArrayList<>(mTabs.size());

        //上一个页面传值是否存在tab list
        boolean isInTabTypeList = Constants.TYPE_ORDER_WAIT_FOR_PAY.equals(mCurrentOrderType)
                || Constants.TYPE_ORDER_WAIT_FOR_DELIVER.equals(mCurrentOrderType)
                || Constants.TYPE_ORDER_WAIT_FOR_RECEIPT.equals(mCurrentOrderType)
                || Constants.TYPE_ORDER_WAIT_FOR_EVALUATE.equals(mCurrentOrderType);

        int defaultTabPos = 0;

        for(int i = 0; i < mTabs.size(); i++) {
            if(isInTabTypeList) {
                if(mTabs.get(i).getCode().equals(mCurrentOrderType)) {
                    defaultTabPos = i;
                }
                tab_my_order.addTab(tab_my_order.newTab().setText(mTabs.get(i).getValue()), mTabs.get(i).getCode().equals(mCurrentOrderType));
            } else {
                tab_my_order.addTab(tab_my_order.newTab().setText(mTabs.get(i).getValue()));
            }

            mFragments.add(MyOrderFragment.newInstance(mTabs.get(i).getCode()));
        }

        //初始化viewpager数据
        mTabFragmentAdapter = new CodeTabFragmentAdapter(getSupportFragmentManager(), mFragments, mTabs);

        vp_my_order.setOffscreenPageLimit(4);
        vp_my_order.setAdapter(mTabFragmentAdapter);
        vp_my_order.setCurrentItem(defaultTabPos);
        tab_my_order.setupWithViewPager(vp_my_order);
    }

    @Override
    protected void initListener() {
        nav_bar_my_order.setOnNavgationBarClickListener(this);
        tab_my_order.addOnTabSelectedListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_my_order;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {//客服
        ContactServiceDialog dialog = ContactServiceDialog.newInstance();

        dialog.show(getSupportFragmentManager(), Constants.TAG_CONTACT_SERVICE_DIALOG);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onOrderListChange(String orderType) {
        if( CommonUtils.getListSize(mTabs) == 5) {
            vp_my_order.setCurrentItem(2);
        }
    }
}
